package data;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.regex.Pattern;

import fields.BankAccount;
import fileProcessing.ParsingException;

public class RoutingNumberEntry extends AbstractDataType<RoutingNumberEntry>{
	
	private int routingNumber;
	private String bank;
	private String fieldType = "Bank Account";
	
	public RoutingNumberEntry(){};
	
	public RoutingNumberEntry(RandomAccessFile raf) throws ParsingException{
		try {
			routingNumber = raf.readInt();
		} catch (IOException e) {
			throw new ParsingException("Error: "+e.getMessage()+" when reading date from table's data file");
		}
	}
	
	public RoutingNumberEntry(String rNum) throws ParsingException{
		if(isValid(rNum)){
			routingNumber = Integer.parseInt(rNum);
			
			bank = fromBank(rNum);
		}
		else
			throw new ParsingException("Phone number not valid.");
	}
	
	public void writeValue(RandomAccessFile raf) throws ParsingException{
		try {
			raf.seek(raf.length());
			raf.writeInt(routingNumber);
			raf.close();
		} catch (IOException e) {
			throw new ParsingException("Error writing to dataFile");
		}
	}
	
	public boolean isValid(String rNum) {
		Pattern pattern = Pattern.compile("((0[0-9])|(1[0-2])|(2[1-9])|(3[0-2])|(6[1-9])|(7[0-2])|80)([0-9]{7})");
		return pattern.matcher(rNum).matches();
	}

	private String fromBank(String rNum) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int compareTo(RoutingNumberEntry o) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public String getType() {
		return fieldType;
	}

}
